package com.lizardmind.haofudoctor.ui.diagnosis.diagnose.userFill

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.EncodeUtils
import com.linxiao.framework.fragment.BaseFragment
import com.linxiao.framework.widget.Section.SectionDecoration
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.other.TypeDocumentsBean
import kotlinx.android.synthetic.main.activity_fill_diagnose_two.*
import kotlinx.android.synthetic.main.fragment_fill_diagnose_two.*
import org.jetbrains.anko.bundleOf
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.support.v4.act

/**
 *
 * Created by Extends on 2017/11/7 10:38
 */
class FillDiagnoseTwoFragment : BaseFragment() {
    lateinit var apt: FillDiagnoseTwoAdapter
    override fun onCreatRootView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater!!.inflate(R.layout.fragment_fill_diagnose_two,container,false)
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        val data = arguments["data"] as TypeDocumentsBean

        ffdt_ll.post {
            val lp = ffdt_view.layoutParams
            lp.height = ffdt_ll.height
            ffdt_view.layoutParams = lp
        }
        ffdt_rv.apply {
            isNestedScrollingEnabled = false
            layoutManager = GridLayoutManager(context, 2)
            apt = FillDiagnoseTwoAdapter(context, data, 2)
            adapter = apt
            addItemDecoration(SectionDecoration(2, apt, false, 0f))
            post { adapter.notifyDataSetChanged() }
        }
        val index = arguments["index"] as Int
        val count = arguments["count"] as Int
        ffdt_type_title.text = data.type_title+"["+(index+1)+"/"+count+"]"

        ffdt_view.onClick {  }
        ffdt_view.visibility = if((act as FillDiagnoseTwoActivity).isClick){
            View.GONE
        }else{View.VISIBLE}
        ffdt_btn0.apply {
            text = "上一步"
            onClick {
                if(index==0){
                    act.finish()
                }else{
                    (act as FillDiagnoseTwoActivity).afdt_hackVP.currentItem = index-1
                }
            }
        }
        ffdt_btn1.apply {
            text = if(index==count-1){
                if((act as FillDiagnoseTwoActivity).isClick){
                    "提交"
                }else{
                    "关闭"
                }

            }else{
                "下一步"
            }

            onClick {
                if(index==count-1){
                    if((act as FillDiagnoseTwoActivity).isClick){
                        (act as FillDiagnoseTwoActivity).submit()
                    }else{
                        act.finish()
                    }
                }else{
                    (act as FillDiagnoseTwoActivity).afdt_hackVP.currentItem = index+1
                }
            }
        }
    }

    /**
     * 获取答案
     */
    fun getAnswer():String{
        return apt.getAnswer()
    }

    fun getSupplement():String{
        return ffdt_et0.text.toString()
    }

    companion object {
       fun getFragment(tdb: TypeDocumentsBean,index:Int,count:Int): FillDiagnoseTwoFragment {
           val tf = FillDiagnoseTwoFragment()
           tf.arguments = bundleOf("data" to tdb,"index" to index,"count" to count)
           return tf
       }
    }
}